package com.zhuiYun.project.api.commodityType.service.impl;

import com.sun.deploy.util.StringUtils;
import com.zhuiYun.project.api.commodity.entity.Commodity;
import com.zhuiYun.project.api.commodity.mapper.CommodityMapper;
import com.zhuiYun.project.api.commodity.model.CommodityModel;
import com.zhuiYun.project.api.commodityType.entity.CommodityType;
import com.zhuiYun.project.api.commodityType.mapper.CommodityTypeMapper;
import com.zhuiYun.project.api.commodityType.model.CommodityTypeModel;
import com.zhuiYun.project.api.commodityType.service.CommodityTypeService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.*;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CommodityTypeServiceImpl
 * @Description TODO
 * Author 高
 * @Date 2023/4/3 20:36
 * @Version 1.0
 **/
@Service
@Slf4j
public class CommodityTypeServiceImpl implements CommodityTypeService {
    @Autowired
    CommodityTypeMapper commodityTypeMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    UploadFileUtils uploadFile;
    /*
     * @Author 高哲
     * @Description 分类分页查询
     * @Date 2023/4/6 09:00
     * @Return pageRequest
     * @Param commodityModel
     */
    @Override
    public CommonResult getCommodityTypeByPage(CommodityTypeModel commodityTypeModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(CommodityType.class);
        Example.Criteria commodityCriteria = example.createCriteria();
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(commodityTypeModel.getTypeSname())){
            commodityCriteria.andLike("typeSname","%"+commodityTypeModel.getTypeSname()+"%");
        }
        //未被删除
        commodityCriteria.andEqualTo("delFlag", 0);
        // 添加倒序排列的代码
        example.orderBy("id").desc();
        //设置分页
        PageResult pageResult = MybatisPageHelper.getPageResult(commodityTypeMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
    /*
     * @Author 高哲
     * @Description 图片添加
     * @Date 2023/4/6 15:11
     * @Return
     * @Param
     */
    @Override
    public int CommodityTypeAdd(MultipartFile[] typeImg, CommodityTypeModel publish) throws CommonException {
        if(publish.getId() == null || publish.getId() == 0){
            publish.setCreateTime(new Date());
            publish.setUpdateTime(new Date());
            publish.setDelFlag(0);
            int i=0;
            if(typeImg!=null){
                List<String> ImgId = new ArrayList<>();
                for (int ids=0;ids <typeImg.length;ids++) {
                    String upl = uploadFile.uploadFile(typeImg[ids]);
                    if (ObjectUtils.isNotEmpty(upl)){
                        ImgId.add(upl);
                    }else{
                        return 0;
                    }
                }
                publish.setImgId(StringUtils.join(ImgId, ","));
            }
            CommodityType commodityType = new CommodityType();
            BeanUtils.copy(publish, commodityType);
            log.info(publish.toString());
            log.info(commodityType.toString());
            i = commodityTypeMapper.insertSelective(commodityType);
            return i;
        } else {
            publish.setUpdateTime(new Date());
            publish.setDelFlag(0);
            int i=0;
            if(typeImg!=null){
                List<String> ImgId = new ArrayList<>();
                for (int ids=0;ids <typeImg.length;ids++) {
                    String upl = uploadFile.uploadFile(typeImg[ids]);
                    if (ObjectUtils.isNotEmpty(upl)){
                        ImgId.add(upl);
                    }else{
                        return 0;
                    }
                }
                publish.setImgId(StringUtils.join(ImgId, ","));
            }
            CommodityType commodityType = new CommodityType();
            BeanUtils.copy(publish, commodityType);
            i = commodityTypeMapper.updateByPrimaryKeySelective(commodityType);
            return i;
        }
    }

    /*
     * @Author 高哲
     * @Description 修改
     * @Date 2023/4/6 18:11
     * @Return
     * @Param
     */
    @Override
    public int CommodityTypeUpdate(MultipartFile[] typeImg, CommodityTypeModel publish) throws CommonException {
        publish.setUpdateTime(new Date());
        publish.setDelFlag(0);
        int i=0;
        if(typeImg!=null){
            List<String> ImgId = new ArrayList<>();
            for (int ids=0;ids <typeImg.length;ids++) {
                String upl = uploadFile.uploadFile(typeImg[ids]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImgId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.setImgId(StringUtils.join(ImgId, ","));
        }
        CommodityType commodityType = new CommodityType();
        BeanUtils.copy(publish, commodityType);
        i = commodityTypeMapper.updateByPrimaryKeySelective(commodityType);
        return i;
    }
    /*
     * @Author 高哲
     * @Description 逻辑删除
     * @Date 2023/4/6 18:11
     * @Return
     * @Param
     */
    @Override
    public CommonResult delCommodityTypeById(Integer id) throws CommonException {
        //创建clientUserEntity
        CommodityType commodityType = new CommodityType();
        //添加条件
        commodityType.setId(id);
        commodityType.setDelFlag(1);
        // 判断是否id为空
        int result = commodityTypeMapper.updateByPrimaryKeySelective(commodityType);
        if (result == 1) {
            return CommonResult.ok("下架成功");
        } else {
            return CommonResult.error("下架失败");
        }

    }
    /*
     * @Author 高哲
     * @Description 树全查
     * @Date 2023/4/18 16:11
     * @Return
     * @Param
     */
    @Override
    public List<CommodityType> getCommodityTypeList() {

        List<CommodityType> CommodityTypeEntities = commodityTypeMapper.selectAll();
//        return CommodityTypeEntities;
        return TreeNode.buildTree(CommodityTypeEntities);
    }
    /*
     * @Author 高哲
     * @Description 树删除
     * @Date 2023/4/18 16:11
     * @Return
     * @Param
     */
    @Override
    public CommonResult delCommodityType1(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = commodityTypeMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

}
